package com.jfinal.ext.interceptor;

import com.google.common.collect.Maps;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.log.Log;
import com.jfinal.render.RenderFactory;
import com.fysports.framework.render.ExceptionRender;

import java.util.Map;


/**
 * 系统异常拦截器<br/>
 * Created by konbluesky<br/>
 * Date : 14-7-30 下午2:57<br/>
 * Project : JdmData_WebServer<br/>
 * Demo:<br/>
 * ExceptionInterceptor exceptionInterceptor = new ExceptionInterceptor();
 * exceptionInterceptor.addMapping(IllegalArgumentException.class, "/exceptions/a.html");
 * exceptionInterceptor.addMapping(IllegalStateException.class, "exceptions/b.html");
 * exceptionInterceptor.setDefault(new ErrorRender("测试系统"));
 */
public class ExceptionInterceptor implements Interceptor {
    private static Log logger =Log.getLog(ExceptionInterceptor.class);
    private ExceptionRender defaultRender;
    private Map<Class<? extends Exception>, ExceptionRender> exceptionMapping = Maps.newHashMap();


    public ExceptionInterceptor setDefault(String defaultView) {
        this.defaultRender = new DefaultExceptionRender(defaultView);
        return this;
    }


    public ExceptionInterceptor setDefault(ExceptionRender defaultRender) {
        this.defaultRender = defaultRender;
        return this;
    }


    public ExceptionInterceptor addMapping(Class<? extends Exception> cause, String view) {
        exceptionMapping.put(cause, new DefaultExceptionRender(view));
        return this;
    }


    public ExceptionInterceptor addMapping(Class<? extends Exception> cause, ExceptionRender view) {
        exceptionMapping.put(cause, view);
        return this;
    }


    @Override public void intercept(Invocation ai) {
        try {
            ai.invoke();
        }
        catch (Exception e) {
            ExceptionRender exceptionRender = matchRender(e);
            if (exceptionRender == null) {
                exceptionRender = defaultRender;
            }
            if (exceptionRender != null) {
                ai.getController().render(exceptionRender.setException(e));
            }
            logger.error(e.getLocalizedMessage(),e);
        }
    }


    private ExceptionRender matchRender(Exception e) {
        Class<?> clazz = e.getClass();
        Class<?> superclass = clazz.getSuperclass();
        ExceptionRender exceptionRender = null;
        while (superclass != null) {
            superclass = clazz.getSuperclass();
            exceptionRender = exceptionMapping.get(clazz);
            if (exceptionRender != null) {
                break;
            }
            clazz = superclass;
        }
        return exceptionRender;
    }


    @SuppressWarnings("serial") private class DefaultExceptionRender extends ExceptionRender {

        public DefaultExceptionRender(String view) {
            this.view = view;
        }


        @Override public void render() {
            RenderFactory.me().getRender(view).setContext(request, response).render();
        }

    }
}